package source.view;

import java.awt.Color;

import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * Displays textual description of events occuring in the game.
 * @author Steve
 *
 */
public class ConsoleViewport extends JScrollPane{
	private JTextPane consoleTextArea;
	private AbstractDocument doc;
	private SimpleAttributeSet attrs;
	
	public ConsoleViewport(){
		super();
		consoleTextArea = new JTextPane();
		consoleTextArea.setEditable(false);
        StyledDocument styledDoc = consoleTextArea.getStyledDocument();
        
        doc = (AbstractDocument)styledDoc;
        
        attrs = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attrs, "SansSerif");
        StyleConstants.setFontSize(attrs, 16);
		setViewportView(consoleTextArea);
	}
	/**
	 * Inserts the string to the beginning of the text area.
	 * @param str
	 */
	public void writeToConsole(String str, Color c)
	{
		consoleTextArea.setCaretPosition(0);
		StyleConstants.setForeground(attrs, c);
        try {
        	doc.insertString(0,str + "\n", attrs);
        } 
        catch (BadLocationException ble) {
        	System.err.println("Couldn't insert initial text.");
        }
	}
}
